﻿CREATE TRIGGER [dbo].[tr_PurchaseBatch_Count]
ON [dbo].[t_PurchaseBatch_Loans]
FOR INSERT,DELETE
AS

IF @@ROWCOUNT = 0 RETURN

UPDATE t_PurchaseBatch SET LoansCount = PB_Ls.Count
FROM t_PurchaseBatch PB 
INNER JOIN (SELECT BatchID,COUNT(*) Count FROM inserted t_PurchaseBatch_Loans GROUP BY BatchID)PB_Ls
	ON PB.ID = PB_Ls.BatchID
